<?php

class Media extends CI_Controller {

	public function index()
	{
		$this->load->helper('url');
		$this->load->view('admin/home');
		
	}
	
	public function retrieve(){
		
		$this->load->helper('url');
		$this->load->model('MediaDAO','dao');
	
		$media = $this->dao->getMediaById($this->uri->segment(4));

		$filename = $media->ruta.$media->archivo;	
		
			// set the download rate limit (=> 20,5 kb/s)
		$download_rate = 20.5;
		if(file_exists($filename) && is_file($filename))
		{
		    header('Cache-control: private');
		    header('Content-Type: application/octet-stream');
		    header('Content-Length: '.filesize($filename));
		    header('Content-Disposition: filename='.$media->archivo);
		
		    flush();
		    $file = fopen($filename, "r");
		    while(!feof($file))
		    {
		        // send the current file part to the browser
		        print fread($file, round($download_rate * 1024));
		        // flush the content to the browser
		        flush();
		        // sleep one second
		        sleep(1);
		    }
		    fclose($file);}
		else {
		    die('Error: The file '.$filename.' does not exist!');
		}
		
	}
	
	
    function publicar(){
    	$this->load->helper('url');
		$this->load->model('MediaDAO','dao');
	
		$id_form 			= $this->input->get_post('id_form',true);
		$id_medio 			= $this->input->get_post('id_medio',true);
		$codigo_publicado 	= $this->input->get_post('codigo_publicado',true);
		
		$this->dao->publicarMedio($id_medio,$codigo_publicado);
		
		redirect('admin/formmonitor/detail/'.$this->uri->segment(5), 'location');
		
    }
	
    function despublicar(){
    	$this->load->helper('url');
		$this->load->model('MediaDAO','dao');
	
		$this->dao->despublicarMedio($this->uri->segment(4));
		
		redirect('admin/formmonitor/detail/'.$this->uri->segment(5), 'location');
		
    }

    function borrar(){
    	$this->load->helper('url');
    	$this->load->model('MediaDAO','dao');

		$id_form 	= $this->uri->segment(4);
		$id_medio 	= $this->uri->segment(5);
		
		$media = $this->dao->getMediaById($id_medio);

		$filename = $media->ruta.$media->archivo;
		
		//echo $filename;

		unlink($filename);
		
		redirect('admin/formmonitor/detail/'.$id_form, 'location');
		
    }
	
    function delete_media_batch(){
    	
    	$this->load->model('MediaDAO','dao');

    	
    	$listado = $this->dao->getAprovedMediaAll();
    	
    	foreach ($listado as $item){
    		$file = $item->ruta.$item->archivo;
    		if(file_exists($file)){
    			unlink($file);
    			echo "<br>".$file." ...deleted!";		
    		}
    		
    		
    		
    	}
    	
    	
    	
    	
    	
    }
    
    
    
	
}



/* End of file media.php */
/* Location: ./application/controllers/forms/media.php */